<?php 
namespace app\mall\controller;

use think\Db;
use think\Request;
use think\Config;
use think\Controller;

class Adv extends Controller{
	public function index(){
		$param = Request()->param();
		$key = $param['key']?:0;
		$num = $param['num']?:6;
		$list = Db::table('sdzx_adv')->alias('adv')
		->join('sdzx_adposition adp','adv.adp_id = adp.adp_id')
		->where("adv.adv_status=1 and adp.adp_key='{$key}'")
		->order('adv.adv_sort desc')
		->select();
		//时间过滤
		foreach ($list as $k => $v) {
			if ($v['start_time'] != 0 && time()<strtotime($v['start_time'])) {
				unset($list[$k]);
				continue;
			}
			if ($v['end_time'] != 0 && time()>strtotime($v['end_time'])) {
				unset($list[$k]);
				continue;
			}
		}
		$list = array_slice($list,0,$num);
		$return_arr = [];
		foreach ($list as $k => $adv) {
			$goods = [];
			if ($adv['realtion_type']=='brand') {
				$goods = Db::table('sdzx_goods')->alias('g')
		    	->field('g.goods_id,g.goods_name,g.brand_name,g.brand_id,g.unit,g.category_id,g.keywords,g.introduction,g.sort,g.label_id,g.picture,max(sval.price) as max_price,min(sval.price) as min_price,max(sval.is_hot) as is_hot,min(sval.sellout) as goods_sellout,max(sval.is_recommend) as is_recommend,max(sval.is_new) as is_new,max(sval.is_cutprice) as is_cutprice,sum(sval.sales) as sales, max(sval.is_hot) as  is_promotion ')
		    	->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id and sval.status=1')
		    	->where("sval.status=1 and g.brand_id in ({$adv['realtion_content']})")
		    	->group('g.goods_id')
		    	->select();

			}elseif ($adv['realtion_type']=='cate') {
				$goods = Db::table('sdzx_goods')->alias('g')
		    	->field('g.goods_id,g.goods_name,g.brand_name,g.brand_id,g.unit,g.category_id,g.keywords,g.introduction,g.sort,g.label_id,g.picture,max(sval.price) as max_price,min(sval.price) as min_price,max(sval.is_hot) as is_hot,min(sval.sellout) as goods_sellout,max(sval.is_recommend) as is_recommend,max(sval.is_new) as is_new,max(sval.is_cutprice) as is_cutprice,sum(sval.sales) as sales, max(sval.is_hot) as  is_promotion ')
		    	->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id and sval.status=1')
		    	->where("sval.status=1 and g.category_id in ({$adv['realtion_content']})")
		    	->group('g.goods_id')
		    	->select();
			}elseif ($adv['realtion_type']=='goods') {
				$goods = Db::table('sdzx_goods')->alias('g')
		    	->field('g.goods_id,g.goods_name,g.brand_name,g.brand_id,g.unit,g.category_id,g.keywords,g.introduction,g.sort,g.label_id,g.picture,max(sval.price) as max_price,min(sval.price) as min_price,max(sval.is_hot) as is_hot,min(sval.sellout) as goods_sellout,max(sval.is_recommend) as is_recommend,max(sval.is_new) as is_new,max(sval.is_cutprice) as is_cutprice,sum(sval.sales) as sales, max(sval.is_hot) as  is_promotion ')
		    	->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id and sval.status=1')
		    	->where("sval.status=1 and g.goods_id in ({$adv['realtion_content']})")
		    	->group('g.goods_id')
		    	->select();
			}else{
				$goods = [];
			}
			$adv['redirect'] = empty($goods)?false:true;
			$return_arr[] = $adv;
		}

		$this->success('success','',$return_arr);
	}
	
	// 点击
	public function advClick(){
		$param = Request()->param();
		$adv_id = $param['adv_id']?:0;
		$adv = Db::name('adv')->where(['adv_id'=>$adv_id])->find();
		//增加点击
		Db::name('adv')->where('adv_id='.$adv_id)->setInc('clicks');
		$goods=[];
		!$adv && $this->success('','',[]);
		if ($adv['realtion_type']=='brand') {
			$goods = Db::table('sdzx_goods')->alias('g')
	    	->field('g.goods_id,g.goods_name,g.brand_name,g.brand_id,g.unit,g.category_id,g.keywords,g.introduction,g.sort,g.label_id,g.picture,max(sval.price) as max_price,min(sval.price) as min_price,max(sval.is_hot) as is_hot,min(sval.sellout) as goods_sellout,max(sval.is_recommend) as is_recommend,max(sval.is_new) as is_new,max(sval.is_cutprice) as is_cutprice,sum(sval.sales) as sales, max(sval.is_hot) as  is_promotion ')
	    	->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id and sval.status=1')
	    	->where("sval.status=1 and g.brand_id in ({$adv['realtion_content']})")
	    	->group('g.goods_id')
	    	->select();

		}elseif ($adv['realtion_type']=='cate') {
			$goods = Db::table('sdzx_goods')->alias('g')
	    	->field('g.goods_id,g.goods_name,g.brand_name,g.brand_id,g.unit,g.category_id,g.keywords,g.introduction,g.sort,g.label_id,g.picture,max(sval.price) as max_price,min(sval.price) as min_price,max(sval.is_hot) as is_hot,min(sval.sellout) as goods_sellout,max(sval.is_recommend) as is_recommend,max(sval.is_new) as is_new,max(sval.is_cutprice) as is_cutprice,sum(sval.sales) as sales, max(sval.is_hot) as  is_promotion ')
	    	->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id and sval.status=1')
	    	->where("sval.status=1 and g.category_id in ({$adv['realtion_content']})")
	    	->group('g.goods_id')
	    	->select();
		}elseif ($adv['realtion_type']=='goods') {
			$goods = Db::table('sdzx_goods')->alias('g')
	    	->field('g.goods_id,g.goods_name,g.brand_name,g.brand_id,g.unit,g.category_id,g.keywords,g.introduction,g.sort,g.label_id,g.picture,max(sval.price) as max_price,min(sval.price) as min_price,max(sval.is_hot) as is_hot,min(sval.sellout) as goods_sellout,max(sval.is_recommend) as is_recommend,max(sval.is_new) as is_new,max(sval.is_cutprice) as is_cutprice,sum(sval.sales) as sales, max(sval.is_hot) as  is_promotion ')
	    	->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id and sval.status=1')
	    	->where("sval.status=1 and g.goods_id in ({$adv['realtion_content']})")
	    	->group('g.goods_id')
	    	->select();
		}else{
			$goods = [];
		}

		//商品规格
		foreach ($goods as $k => $v) {
    		$tmp = Db::connect([],'sval')->name('goods_specval')->where(['goods_id'=>$v['goods_id'],'status'=>1])->order('price asc')->select();
            $cur = array_shift($tmp);
            $goods[$k] = array_merge($goods[$k],$cur);
            if (sizeof($tmp)>0) {
                $goods[$k]['list']=$tmp;
            }
    	}

    	$this->success('success','',['list'=>$goods,'adv'=>$adv]);
	}
}
?>